package io.github.rascaler.dcenter.pojo;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @ClassName FixedArrayList
 * @Description TODO
 * @Author wurenqing
 * @Date 2023-11-24 15:07
 */
public class FixedArrayList<T> extends ArrayList<T> {

    public final static int MAX_SIZE = 1500;

    public FixedArrayList() {
        this.maxSize = MAX_SIZE;
    }

    public FixedArrayList(Collection<T> collections) {
        super(collections);
        this.maxSize = MAX_SIZE;
    }
    public FixedArrayList(int maxSize) {
        this.maxSize = maxSize;
    }

    public FixedArrayList(Collection<T> collections, int maxSize) {
        this.maxSize = maxSize;
        this.addAll(collections);
    }

    private int maxSize;

    public void removeOverflows() {
        int diff = size() - maxSize;
        if (diff > 0) {
            this.removeRange(0, diff);
        }
    }

    public boolean add(T e) {
        boolean result = super.add(e);
        this.removeOverflows();
        return result;
    }

    public boolean addAll(Collection<? extends T> c) {
        boolean result = super.addAll(c);
        this.removeOverflows();
        return result;
    }

    public T removeTail() {
        return super.remove(size() - 1);
    }



}